package yxy.game.pm2.command.passbarrier;

import org.redisson.api.RLock;
import yxy.cherry.data.DataCenter;
import yxy.cherry.data.bean.passbariestepreward;
import yxy.cherry.game.base.message.MessageEmitterHandler;
import yxy.cherry.game.base.message.annotation.MessageSource;
import yxy.cherry.game.base.message.annotation.MessageType;
import yxy.game.pm2.Result;
import yxy.game.pm2.bean.Currency;
import yxy.game.pm2.bean.currency.Currencies;
import yxy.game.pm2.bean.mq.CCReason;
import yxy.game.pm2.command.Command;
import yxy.game.pm2.message.PackageMessage;
import yxy.game.pm2.message.PassBarrierMessage;
import yxy.game.pm2.module.CurrencyHandle;
import yxy.game.pm2.module.passbarrier.HeadBox;
import yxy.game.pm2.module.passbarrier.PassbarrierPlayerModule;

import java.util.Collection;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;


/****************************************
 * @name CMD_PassBarrierScoreRewardReceive
 * @description 斩关夺隘积分奖励领取
 * @author 梁健彬
 * @since 2022-09-05
 ***************************************/
@MessageType(proto = Command.PassBarrierScoreRewardReceive, classtype = PassBarrierMessage.PassBarrierScoreRewardReceiveRequest.class)
public class CMD_PassBarrierScoreRewardReceive extends Command<PassBarrierMessage.PassBarrierScoreRewardReceiveRequest> {

    @Override
    public void execute(MessageSource messageSource, PassBarrierMessage.PassBarrierScoreRewardReceiveRequest request) throws Exception {
        String playerCode = messageSource.getTag();
        int id = request.getId();
        PassBarrierMessage.PassBarrierScoreRewardReceiveResponse.Builder response = PassBarrierMessage.PassBarrierScoreRewardReceiveResponse.newBuilder();
        RLock lock = PassbarrierPlayerModule.getRedisLock(PassbarrierPlayerModule.getlockKey(playerCode));

        try {
            PassbarrierPlayerModule module = PassbarrierPlayerModule.get(playerCode);
            if (module == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            passbariestepreward data = DataCenter.getData(id, passbariestepreward.class);
            if (data == null) {
                send(messageSource, response, Result.DataError);
                return;
            }

            HashMap<Integer, HeadBox> headBoxMsg = module.getHeadBoxMsg();
            if (!headBoxMsg.containsKey(id)) {
                send(messageSource, response, Result.DataError);
                return;
            }

            HeadBox headBox = headBoxMsg.get(id);
            if (headBox.getReward()) {
                send(messageSource, response, Result.PassBarrierAlreadyReward);
                return;
            }

            if (module.getTotalScore() < headBox.getScore()) {
                send(messageSource, response, Result.PassBarrierCantReward);
                return;
            }

            //todo 领取
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
                CurrencyHandle currencyHandle = new CurrencyHandle();
                Currencies curs = new Currencies(Currency.create(data.getRewardtype(), data.getRewardid(), data.getRewardnum()));
                Collection<PackageMessage.Item> items = new CurrencyHandle().packItems(curs);
                currencyHandle.addCurrency(playerCode, CCReason.过关斩将, curs);
                response.addAllItem(items);
                headBox.setReward(true);
                module.set();

                //todo 头部宝箱信息
                for (HeadBox value : module.getHeadBoxMsg().values()) {
                    PassBarrierMessage.HeadBox.Builder builder = PassBarrierMessage.HeadBox.newBuilder();
                    builder.setDataId(value.getId());
                    builder.setIsReward(value.getReward());
                    builder.setScore(value.getScore());
                    response.addBoxes(builder);
                }
            }

            send(messageSource, response, Result.Success);
        } catch (Exception e) {
            e.printStackTrace();
            send(messageSource, response, Result.DataError);
        } finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }
    }

    private void send(MessageSource messageSource, PassBarrierMessage.PassBarrierScoreRewardReceiveResponse.Builder response, Result result) {
        response.setResult(result.packResult());
        MessageEmitterHandler.getInstance().send(messageSource, response.build());
    }


}
